//
//  ViewController.m
//  day6作业 画板
//
//  Created by tyw on 15/8/5.
//  Copyright (c) 2015年 tyw. All rights reserved.
//

#import "ViewController.h"
#import "MyButton.h"
#import "DrawView.h"


#define kWidth [UIScreen mainScreen].bounds.size.width
#define kHeight [UIScreen mainScreen].bounds.size.height
#define navBarHeight 64
#define tabBarHeight 49

@interface ViewController () {
    UIView* _grayView;      //顶部的灰色视图，承载工具栏
    UIView* _redLineView1;  //工具栏下面的红色标记
    UIView* _redLineView2;  //颜色下面的标记
    UIView* _redLineView3;  //线宽下面的标记
    UIView* _colorView;     //承载各颜色按钮的视图
    BOOL _colorViewIsShow;  //标记颜色视图是否显示
    UIView* _lineWidthView; //线宽视图
    
    DrawView* _drawView;    //画图板
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    _grayView = [[UIView alloc] initWithFrame:CGRectMake(0, 30, kWidth, 150)];
    _grayView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:_grayView];
    
    
    [self _createToolButtons];
    [self _createColorView];
    [self _createLineWidthView];
    [self _createDrawView];
}

#pragma -mark createSubViews

- (void)_createToolButtons {
    NSArray* titleArray = @[@"颜色",@"线宽",@"橡皮",@"撤销",@"清屏"];
    
    for (int i = 0; i < titleArray.count; i++) {
        MyButton* button = [[MyButton alloc] initWithFrame:CGRectMake(i*kWidth/5, 0, kWidth/5, 60)];
        button.tag = (i+1)*100;
        button.title = titleArray[i];
        [_grayView addSubview:button];
        button.block = ^ {
            [UIView animateWithDuration:0.3 animations:^{//红色标记移动动画
                [_redLineView1 setCenter:CGPointMake(i*kWidth/5+kWidth/10, 62.5)];
            }];
            if (button.tag == 100) {        //颜色
                _colorView.hidden = NO;
                _redLineView2.hidden = NO;
                _colorViewIsShow = YES;
                _lineWidthView.hidden = YES;
                _redLineView3.hidden = YES;
            } else if (button.tag == 200) { //线宽
                _colorView.hidden = YES;
                _redLineView2.hidden = YES;
                _colorViewIsShow = NO;
                _lineWidthView.hidden = NO;
                _redLineView3.hidden = NO;
            } else if (button.tag == 300) { //橡皮
                _drawView.color = [UIColor whiteColor];
            } else if (button.tag == 400) { //撤销
                [_drawView undo];
            } else {                        //清屏
                [_drawView clear];
            }
        };
    }
    
    _redLineView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 60, kWidth/5, 5)];
    [_grayView addSubview:_redLineView1];
    _redLineView1.backgroundColor = [UIColor redColor];
}

- (void)_createColorView {
    _colorView = [[UIView alloc] initWithFrame:CGRectMake(0, 70, kWidth, 70)];
    [_grayView addSubview:_colorView];
    _colorViewIsShow = YES;
    
    
    NSArray* colorArray = @[[UIColor blackColor],[UIColor lightGrayColor],[UIColor redColor],[UIColor greenColor],[UIColor blueColor],[UIColor yellowColor],[UIColor orangeColor],[UIColor purpleColor],[UIColor brownColor]];
    CGFloat width = kWidth/9-4;
    CGFloat height = 60;
    for (int i = 0; i < colorArray.count; i++) {
        MyButton* button = [[MyButton alloc] initWithFrame:CGRectMake(2+(kWidth/9)*i, 5, width, height)];
        [_colorView addSubview:button];
        button.tag = (i+1)*100;
        button.backgroundColor = colorArray[i];
        button.block = ^ {
            [UIView animateWithDuration:0.3 animations:^ {
                [_redLineView2 setFrame:CGRectMake(2+(kWidth/9)*i, 140, width, 5)];
                _redLineView2.backgroundColor = colorArray[i];
            }];
            _drawView.color = colorArray[i];
        };
    }
    
    _redLineView2 = [[UIView alloc] initWithFrame:CGRectMake(2, 140, width, 5)];
    _redLineView2.backgroundColor = [UIColor blackColor];
    [_grayView addSubview:_redLineView2];
}

- (void)_createLineWidthView {
    _lineWidthView = [[UIView alloc] initWithFrame:CGRectMake(0, 70, kWidth, 70)];
    [_grayView addSubview:_lineWidthView];
    _lineWidthView.hidden = YES;
    
    NSArray* widthArray = @[@"1点",@"5点",@"10点",@"15点",@"20点",@"30点"];
    NSArray* width = @[@1,@5,@10,@15,@20,@30];
    
    for (int i = 0; i < widthArray.count; i++) {
        MyButton* button = [[MyButton alloc] initWithFrame:CGRectMake(i*kWidth/6, 0, kWidth/6, 60)];
        button.title = widthArray[i];
        button.tag = (i+1)*100;
        [_lineWidthView addSubview:button];
        button.block = ^ {
            [UIView animateWithDuration:0.3 animations:^ {
                [_redLineView3 setFrame:CGRectMake(i*kWidth/6, 140, kWidth/6, 5)];
            }];
            _drawView.lineWidth = [width[i] integerValue];
        };
    }
    
    _redLineView3 = [[UIView alloc] initWithFrame:CGRectMake(0, 140, kWidth/6, 5)];
    _redLineView3.backgroundColor = [UIColor redColor];
    [_grayView addSubview:_redLineView3];
    _redLineView3.hidden = YES;
}

- (void)_createDrawView {
    _drawView = [[DrawView alloc] initWithFrame:CGRectMake(0, 180, kWidth, kHeight-180)];
    [self.view addSubview:_drawView];
}



#pragma -mark others

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
